module.exports = {
	data() {
		return {}
	},
	methods: {
		toPage(url) {
			uni.navigateTo({
				url
			})
		},
		regTel(tel) {
			// 电话号码正则(手机号正则)
			let checkTelReg = /^[0-9]{11}$/;
			let identityReg =
			/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
			return checkTelReg.test(tel);
		},
		loginToPage(url) {
			if (!this.$db.get("userToken")) {

				this.$common.errorToShow('请先登录')
				wx.navigateTo({
					url: '/pages/login/login'
				})
				return
			}
			uni.navigateTo({
				url
			})
		},
		toGetPhone() {
			uni.navigateTo({
				url: '/pages/login/login'
			})
		},

		sortCompare(property) {
			return function(a, b) {
				var value1 = a[property];
				var value2 = b[property];
				return value2 - value1;
			}
		},
		toBack() {
			uni.navigateBack()
		},
		toHome() {
			uni.switchTab({
				url: '/pages/index/index'
			})
		},
		toCart() {
			uni.switchTab({
				url: '/pages/cart/index'
			})
		},
		goArticleInfo: function(columnid) { //打开文章
			console.log(columnid, 121212)
			this.$api.articleInfo({
				id: columnid
			}, res => {
				if (res.code == 200) {

					let info = res.data;


					if (!info.href) {

						uni.navigateTo({
							url: `/pages/single/single?id=${info.id}`
						})
						return
					}
					if (info.href.indexOf('http') >= 0) {

						if (info.author) {

							wx.openCustomerServiceChat({
								extInfo: {
									url: info.href
								},
								corpId: info.author,
								success(res) {
									console.log(res, 12);
								},
								complete(res) {
									console.log(res, 12);
								}
							});
							return
						}
						uni.navigateTo({
							url: `/pages/webview/webview?src=${info.href}&id=${info.id}&title=${info.title}`
						})
					}
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			})
		},
		// 当前时间判断
		timeDue: function(time) {
			let curTime = new Date().getTime()
			let dueTime = new Date((time.replaceAll('-', '/'))).getTime()
			if (curTime - dueTime > 0) {
				return true
			} else {
				return false
			}
		},
		prevImg(attr) {

			uni.previewImage({
				urls: attr
			});
		},
		goSearch: function() { //打开搜索页
			wx.navigateTo({
				url: "/pages/search/search"
			})
		},
		disabledEvent: function() {
			return false;
		},
		Deduplication: function(arr1, arr2) { //合并去重
			return [...new Set(arr1.concat(arr2))]
		},
		differenceSet: function(arr1, ...arrs) { //差集
			var all = [];
			for (var i = 0; i < arrs.length; i++) {
				all = all.concat(arrs[i])
			};
			return arr1.filter(function(a) {
				return !all.includes(a)
			});
		},
		dateFormatter: function(date, str) { //时间格式化
			console.log(date, 11111111111)
			if (!date) return str;
			var year = new Date(date).getFullYear();
			var month = (new Date(date).getMonth() + 1) > 9 ? (new Date(date).getMonth() + 1) : ('0' + (
				new Date(date).getMonth() + 1));
			var day = new Date(date).getDate() > 9 ? new Date(date).getDate() : ('0' + new Date(date)
				.getDate());
			var hours = new Date(date).getHours() > 9 ? new Date(date).getHours() : ('0' + new Date(date)
				.getHours());
			var minutes = new Date(date).getMinutes() > 9 ? new Date(date).getMinutes() : ('0' + new Date(date)
				.getMinutes());
			var seconds = new Date(date).getMilliseconds() > 9 ? new Date(date).getMilliseconds() : ('0' +
				new Date(date).getMilliseconds());
			return str.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('HH', hours)
				.replace('mm', minutes).replace('ss', seconds);
		},
		remainder: function(date) { //剩余时间
			var res = {
				days: 0,
				hours: 0,
				minutes: 0,
				seconds: 0
			};
			var now = new Date().getTime();
			var end = new Date(date).getTime();
			if (end <= now) return res;
			res.days = parseInt((end - now) / (24 * 60 * 60 * 1000));
			res.hours = parseInt((end - now - res.days * (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
			res.minutes = parseInt((end - now - res.days * (24 * 60 * 60 * 1000) - res.hours * (60 * 60 *
				1000)) / (60 * 1000));
			res.seconds = parseInt((end - now - res.days * (24 * 60 * 60 * 1000) - res.hours * (60 * 60 *
				1000) - res.minutes * (60 * 1000)) / 1000);
			return res;
		},
		replaceWith: function(str, startLen, endLen, str1) { //字符替换，str:需要替换的字符,startLen:起始长度,endLen:结尾长度,str1:替换字符
			var res = str;
			var arr = str.split('');
			if (startLen + endLen < str.length) {
				res = arr.splice(0, startLen).join('') + str1 + arr.splice(arr.length - endLen).join('')
			};
			return res;
		},
		openSinglePage: function(id) {
			wx.navigateTo({
				url: "/pages/single/single?id=" + id
			})
		},
		groupAndSortByMonth: function(dateArray) {
			// 使用 reduce 方法将日期按年份和月份分组
			const groupedByYearMonth = dateArray.reduce((result, obj) => {
				const date = new Date(obj.date);
				const year = date.getFullYear();
				const month = date.getMonth() + 1;
				if (!result[year]) {
					result[year] = {};
				}
				if (!result[year][month]) {
					result[year][month] = [];
				}
				result[year][month].push(obj);
				return result;
			}, {});
			// 对每个年份和月份的对象数组进行排序
			var res = [];
			let yearkeys = Object.keys(groupedByYearMonth).sort((a, b) => {
				return b - a
			});
			for (var i = 0; i < yearkeys.length; i++) {
				var tmp = {
					name: yearkeys[i],
					list: []
				};
				let monthkeys = Object.keys(groupedByYearMonth[yearkeys[i]]).sort((a, b) => {
					return b - a
				});
				for (var j = 0; j < monthkeys.length; j++) {
					tmp.list.push({
						name: monthkeys[j],
						list: groupedByYearMonth[yearkeys[i]][monthkeys[j]].sort((a, b) => {
							const dateA = new Date(a.date);
							const dateB = new Date(b.date);
							return dateB - dateA;
						})
					});
				};
				res.push(tmp);
			};
			return res;
		},
		getDateStatus: function(startDate, endDate) {
			var start = new Date(startDate);
			var end = new Date(endDate);
			var current = new Date();
			if (current < start) {
				return 0; //未开始
			} else if (current > end) {
				return 2; //已结束
			} else {
				return 1; //活动中
			}
		}

	}
}